﻿using System;
using System.Linq;
using System.Xml.Linq;
using LinqToYql;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using YQLDataProvider;
using YQLDataProvider.Models;
using YQLDataProvider.YqlDataLoaders;

namespace DataProviderIntegrationTests.LinqToYQLTests
{
    [TestClass]
    public class LinqToYQLTests
    {
        [TestMethod]
        public void LinqToYqlIndustryIntegrationTestForOneIndustry()
        {
            IQueryable<YqlIndustry> query =
                new QuerableYQLData<YqlIndustry, XDocument, YqlIndustry>(new QuerySettings("yahoo.finance.industry"),
                                                                        new XmlIndustryDataLoader());
            var result = query.Where(ind =>ind.Key == 112).Select(x=>new {x.Name});
            Assert.AreEqual(1,result.Count());
            foreach (var pind in result)
                Assert.IsNotNull(pind);
        }

        [TestMethod]
        public void LinqToYqlIndustryIntegrationTestForSomeIndustries()
        {
            IQueryable<YqlIndustry> query =
                new QuerableYQLData<YqlIndustry, XDocument, YqlIndustry>(new QuerySettings("yahoo.finance.industry"),
                                                                        new XmlIndustryDataLoader());
            var result = query.Where(ind =>new[]{112,113}.Contains(ind.Key)).Select(x => x);
            Assert.AreEqual(2, result.Count());
            foreach (var yqlIndustry in result)
                Assert.IsNotNull(yqlIndustry.Companies);
        }

        [TestMethod]
        public void LinqToYqlSectorsIntegrationTest()
        {
            IQueryable<YqlIndustrySector> query =
                new QuerableYQLData<YqlIndustrySector, XDocument, YqlIndustrySector>(
                    new QuerySettings("yahoo.finance.sectors "), new XmlSectorsDataLoader());
            var result = query.Select(x => x);
            foreach (var yqlIndustrySector in result)
                Assert.IsNotNull(yqlIndustrySector);
        }

        [TestMethod]
        public void LinqToYqlSectorsIntegrationTest_TestNewOperator()
        {
            IQueryable<YqlIndustrySector> query =
                new QuerableYQLData<YqlIndustrySector, XDocument, YqlIndustrySector>(
                    new QuerySettings("yahoo.finance.sectors "), new XmlSectorsDataLoader());
            var result = from sectors in query select new YqlIndustrySector {Name = sectors.Name, Id = Guid.NewGuid()};
            foreach (var yqlIndustrySector in result)
                Assert.IsNotNull(yqlIndustrySector);
        }

        [TestMethod]
        public void LinqToYqlTestGetQuotesForCompanyIntegrationTest()
        {
            IQueryable<YqlIndustry> queryInd =
                new QuerableYQLData<YqlIndustry, XDocument, YqlIndustry>(new QuerySettings("yahoo.finance.industry"),
                                                                        new XmlIndustryDataLoader());
            var resultInd = queryInd.Where(ind1 =>ind1.Key == 112).Select(x=>x).FirstOrDefault();
            var company =  resultInd.Companies.FirstOrDefault();
            
            IQueryable<YqlQuote> query =
                new QuerableYQLData<YqlQuote, XDocument, YqlQuote>(new QuerySettings("yahoo.finance.quotes"),
                                                                  new XmlQuoteDataLoader());
            var result = query.Where(ind => ind.Symbol == company.Symbol).Select(x => x);
            Assert.AreEqual(1,result.Count());
            foreach (var pind in result)
                Assert.IsNotNull(pind);
        }

        [TestMethod]
        public void LinqToYqlTestGetQuotesForSomeCompaniesIntegrationTest()
        {
            IQueryable<YqlIndustry> queryInd =
               new QuerableYQLData<YqlIndustry, XDocument, YqlIndustry>(new QuerySettings("yahoo.finance.industry"),
                                                                       new XmlIndustryDataLoader());
            var resultInd = queryInd.Where(ind1 => ind1.Key == 112).Select(x => x).FirstOrDefault();
            var companySymbols = resultInd.Companies.Select(x=>x.Symbol);

            IQueryable<YqlQuote> query =
                new QuerableYQLData<YqlQuote, XDocument, YqlQuote>(new QuerySettings("yahoo.finance.quotes"),
                                                                  new XmlQuoteDataLoader());
            var result = query.Where(ind => companySymbols.Contains(ind.Symbol)).Select(x => x);
            var count = result.Count();
            Assert.AreEqual(companySymbols.Count(), count);
            foreach (var yqlQuote in result)
                Assert.IsNotNull(yqlQuote);
            
        }

        [TestMethod]
        public void LinqToYqlTestGetXchangeRateByPair()
        {
            IQueryable<YqlXchangeRate> query =
                new QuerableYQLData<YqlXchangeRate, XDocument, YqlXchangeRate>(
                    new QuerySettings("yahoo.finance.xchange"), new XmlXchangeDataLoader());
            var result = (from yqlXchangeRate in query where yqlXchangeRate.Pair.PairId == "EURUSD" select yqlXchangeRate).ToList();
            Assert.AreEqual(1,result.Count());

        }

        [TestMethod]
        public void LinqToYqlTestGetEmptyCollectionIfContainsCollectionIsEmpty()
        {
            IQueryable<YqlIndustry> queryInd =
               new QuerableYQLData<YqlIndustry, XDocument, YqlIndustry>(new QuerySettings("yahoo.finance.industry"),
                                                                       new XmlIndustryDataLoader());
            var resultInd = queryInd.Where(ind1 => new int[0].Contains(ind1.Key)).Select(x => x);
            Assert.IsNotNull(resultInd);
            Assert.IsFalse(resultInd.Any());
        }
    }
}
